﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;

namespace HotelManagement._03.DAO
{
    class UserDAO
    {
        DbConnection m_conn;
        SqlCommand m_command;
        SqlDataAdapter m_adap;

        public UserDAO()
        {
            m_conn = new DbConnection();
        }
        public DataTable ViewUser(string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
        public string ViewRoleOfUser(int ip_Id,string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_Id;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            if (v_dt.Rows.Count == 0) return "";
            else return v_dt.Rows[0]["RoleDescription"].ToString();

        }
        public int ViewUserByAccount(string ip_user,string ip_password, string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = ip_user;
            m_command.Parameters.Add("@Password", SqlDbType.NVarChar).Value = ip_password;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            if (v_dt.Rows.Count == 0) return 0;
            else return int.Parse(v_dt.Rows[0]["UserID"].ToString());

        }
        public DataTable ViewUserById(int ip_Id,string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_Id;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
        public bool AddUser(User ip_User, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                //m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_User.UserID;
                m_command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ip_User.Email;
                m_command.Parameters.Add("@Note", SqlDbType.NVarChar).Value = ip_User.Note;
                m_command.Parameters.Add("@FullName", SqlDbType.NVarChar).Value = ip_User.FullName;
                m_command.Parameters.Add("@Password", SqlDbType.NVarChar).Value = ip_User.Password;
                m_command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = ip_User.UserName;
                m_command.Parameters.Add("@RoleID", SqlDbType.BigInt).Value = ip_User.RoleID;
                m_command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = ip_User.Gender;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;

        }
        public bool UpdateUser(User ip_User, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_User.UserID;
                m_command.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ip_User.Email;
                m_command.Parameters.Add("@Note", SqlDbType.NVarChar).Value = ip_User.Note;
                m_command.Parameters.Add("@FullName", SqlDbType.NVarChar).Value = ip_User.FullName;
                m_command.Parameters.Add("@Password", SqlDbType.NVarChar).Value = ip_User.Password;
                m_command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = ip_User.UserName;
                m_command.Parameters.Add("@RoleID", SqlDbType.BigInt).Value = ip_User.RoleID;
                m_command.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = ip_User.Gender;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;

        }
        public bool UpdatePassword(User ip_User, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_User.UserID;
                m_command.Parameters.Add("@Password", SqlDbType.NVarChar).Value = ip_User.Password;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
                return false;
            }
            return true;

        }
        public bool DeleteUser(User ip_User, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@UserID", SqlDbType.BigInt).Value = ip_User.UserID;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Nghiệp vụ người sử dụng này thực hiện đã được lưu lại. Bạn không thể xóa thông tin !  !");
                return false;
            }
            return true;
        }
        public DataTable SearchUser(string ip_KeyWord, string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            m_command.Parameters.Add("@KeyWord", SqlDbType.NVarChar).Value = ip_KeyWord;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;

        }
    }
}
